// pages/courseList/courseList.js
const app = getApp()
const quote = app.globalData.quote;
const api = quote.api;
const mta = require('../../../utils/mta_analysis.js'); //引入腾讯埋点文件
Page({

    /**
     * 页面的初始数据
     */
    data: {
        unitIndex: app.globalData.unitIndex,
        imgUrl: app.globalData.imgUrl,
        isShowUnlock: false,
        isBrang: app.globalData.isBrang,
        list: '',
        windowWidth: app.globalData.windowWidth,
        px: null, //
        str: '',
        add: false,
        android: false,
        ios: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        if (options.addId == "1" && app.globalData.system == "and") {
            that.setData({
                add: true,
                android: true
            })

        } else if (options.addId == "1" && app.globalData.system == "ios") {
            that.setData({
                add: true,
                ios: true
            })
        }
        wx.showLoading({
            title: '加载中...',
        })
        mta.Page.init(); //初始化腾讯埋点
        that.setData({
            px: 366 / 750 * this.data.windowWidth,
            unitIndex: app.globalData.unitIndex,
        })
        that.getListNextData()
    },
    //确定加入引导页
    determine() {

        wx.setStorage({
          key: 'removes',
          data: 'removes'
        });
        this.setData({
            add: false
        })
    },
    //获取单元下面的列表数据
    getListNextData() {
        var that = this
        var dataDic = {
            OpenId: app.globalData.openId,
        };
        api.methodBasicEnglish_UnlockInformation({
            dataDic,
            success: function (res) {
                console.log(res, '55')
                that.setData({
                    list: res.data.data.UnitList[app.globalData.unitIndex].ModuleList
                })
                that.data.list.forEach(item => {
                    item.open = false
                })
                console.log(that.data.list)
                that.setData({
                    list: that.data.list
                })
                wx.hideLoading()
            }
        })
    },
    //返回上一页
    goback() {
        wx.reLaunch({
            url: '/pages/indexPackage/index/index?goback=true',
        })
    },

    //上拉下拉
    kindToggle: function (e) {
        var that = this
        var status = e.currentTarget.dataset.status;
        console.log(e)
        if (status == 1) {
            var id = e.currentTarget.id;
            var moduleId = e.currentTarget.dataset.moduleid;
            var str = e.currentTarget.dataset.str;
            app.globalData.courseName = e.currentTarget.dataset.str;
            app.globalData.moduleId = moduleId
            that.setData({
                str: str
            })
            // 使用id获取打开的子列表
            for (var i = 0, len = that.data.list.length; i < len; ++i) {

                if (i == id) {
                    that.data.list[i].open = !that.data.list[i].open
                } else {
                    that.data.list[i].open = false
                }
            }
            that.setData({
                list: that.data.list,
            });
        } else {
            console.log('未解锁')
            that.setData({
                isShowUnlock: true
            })
        }

    },
    //关卡未解锁提示
    confirm: function () {
        var that = this
        that.setData({
            isShowUnlock: false
        })
    },

    emptyFunc: function (e) {
        let that = this
        console.log(e)
        var status = e.currentTarget.dataset.status
        var index = parseInt(e.currentTarget.dataset.id)
        console.log(index)
        if (status == 1) {
            if (index == 0) {
                wx.navigateTo({
                    url: '../../../pages/indexPackage/picture/picture?str=' + that.data.str,
                })
            } else if (index == 1) {
                wx.showLoading({
                    title: '加载中...',
                })
                that.getGameStatus()
            } else {
                wx.showLoading({
                    title: '加载中...',
                })
                that.getTaskStatus()
            }
            app.aldstat.sendEvent('选择课程', {
                'course': '解锁状态-' + that.data.str + '-' + index
            });
            mta.Event.stat("course_click", {
                'course': '解锁状态-' + that.data.str + '-' + index
            });

        } else {
            app.aldstat.sendEvent('选择课程', {
                'course': '未解锁状态-' + that.data.str + '-' + index
            });
            mta.Event.stat("course_click", {
                'course': '未解锁状态-' + that.data.str + '-' + index
            });
            that.setData({
                isShowUnlock: true
            })
        }

    },
    //获取任务状态
    getTaskStatus: function () {
        var that = this
        var dataDic = {
            ModuleId: app.globalData.moduleId,
        };
        api.methodBasicEnglish_GetTask({
            dataDic: dataDic,
            success: function (res) {
                console.log('数据请求成功', res)
                app.globalData.taskStatus = res.data.data
                wx.hideLoading()
                if (res.data.data.TaskOne == true) {
                    wx.reLaunch({
                        url: '/pages/taskPackage/pages/taskOne/taskOne',
                    })
                    return
                } else if (res.data.data.TaskTwo == true) {
                    wx.reLaunch({
                        url: '/pages/taskPackage/pages/taskTwo/taskTwo',
                    })
                    return
                } else if (res.data.data.TaskThree == true) {
                    wx.reLaunch({
                        url: '/pages/taskPackage/pages/taskThree/taskThree',
                    })
                    return
                } else if (res.data.data.TaskFour == true) {
                    wx.reLaunch({
                        url: '/pages/taskPackage/pages/taskFour/taskFour',
                    })
                    return
                }
            },
            fail: function (err) {
                console.log('数据请求失败', err)
            }
        })

    },
    //获取游戏状态
    getGameStatus: function () {

        var that = this
        var dataDic = {
            ModuleId: app.globalData.moduleId,
        };
        api.methodBasicEnglish_GetGameStatus({
            dataDic: dataDic,
            success: function (res) {
                app.globalData.gameStatus = res.data.data
                wx.hideLoading()
                console.log('数据请求成功', res)
                if (res.data.data.GameThree == true) {
                    wx.reLaunch({
                        url: '/pages/gamePackage/pages/gameFunnel/gameFunnel',
                    })
                    return
                } else if (res.data.data.GameOne == true) {
                    wx.reLaunch({
                        url: '/pages/gamePackage/pages/gamePicmate/gamePicmate',
                    })
                    return
                } else if (res.data.data.GameTwo == true) {
                    wx.reLaunch({
                        url: '/pages/gamePackage/pages/gameWordspell/gameWordspell',
                    })
                    return
                }
            },
            fail: function (err) {
                console.log('数据请求失败', err)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            path: 'pages/indexPackage/index/index',
            success: function (res) {
                console.log('转发成功', res);
            },
            fail: function (err) {
                console.log('转发失败', err)
            }
        }
    }
})